// 红花排行榜页面
Page({
  data: {
    students: [],
    loading: false,
    error: null,
    currentFilter: 'all', // all, week, month
    searchKeyword: ''
  },

  onLoad() {
    console.log('红花排行榜页面加载');
    this.loadStudentRanking();
  },

  onShow() {
    // 页面显示时刷新数据
    this.loadStudentRanking();
  },

  // 加载学生红花排行榜
  async loadStudentRanking() {
    this.setData({ loading: true, error: null });
    
    try {
      // 模拟数据，实际项目中应该从服务器获取
      const students = [
        {
          id: 1,
          name: '张三',
          avatar: '👦',
          totalFlowers: 25,
          weekFlowers: 5,
          monthFlowers: 15,
          rank: 1,
          recentActivity: '完成数学作业'
        },
        {
          id: 2,
          name: '李四',
          avatar: '👧',
          totalFlowers: 22,
          weekFlowers: 3,
          monthFlowers: 12,
          rank: 2,
          recentActivity: '积极参与课堂讨论'
        },
        {
          id: 3,
          name: '王五',
          avatar: '👦',
          totalFlowers: 18,
          weekFlowers: 4,
          monthFlowers: 10,
          rank: 3,
          recentActivity: '帮助同学解答问题'
        },
        {
          id: 4,
          name: '赵六',
          avatar: '👧',
          totalFlowers: 15,
          weekFlowers: 2,
          monthFlowers: 8,
          rank: 4,
          recentActivity: '按时完成作业'
        },
        {
          id: 5,
          name: '钱七',
          avatar: '👦',
          totalFlowers: 12,
          weekFlowers: 1,
          monthFlowers: 6,
          rank: 5,
          recentActivity: '课堂表现良好'
        }
      ];
      
      this.setData({ 
        students: students,
        loading: false 
      });
    } catch (error) {
      console.error('获取红花排行榜失败:', error);
      this.setData({ 
        error: '加载失败，请重试',
        loading: false 
      });
      
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
    }
  },

  // 设置筛选条件
  setFilter(e) {
    const filter = e.currentTarget.dataset.filter;
    this.setData({ currentFilter: filter });
    this.filterStudents();
  },

  // 筛选学生
  filterStudents() {
    const { students, currentFilter, searchKeyword } = this.data;
    let filteredStudents = [...students];
    
    // 按时间筛选
    if (currentFilter === 'week') {
      filteredStudents = filteredStudents.sort((a, b) => b.weekFlowers - a.weekFlowers);
    } else if (currentFilter === 'month') {
      filteredStudents = filteredStudents.sort((a, b) => b.monthFlowers - a.monthFlowers);
    } else {
      filteredStudents = filteredStudents.sort((a, b) => b.totalFlowers - a.totalFlowers);
    }
    
    // 按搜索关键词筛选
    if (searchKeyword) {
      filteredStudents = filteredStudents.filter(student => 
        student.name.includes(searchKeyword)
      );
    }
    
    this.setData({ students: filteredStudents });
  },

  // 搜索输入
  onSearchInput(e) {
    this.setData({ searchKeyword: e.detail.value });
    this.filterStudents();
  },

  // 清除搜索
  clearSearch() {
    this.setData({ searchKeyword: '' });
    this.filterStudents();
  },

  // 查看学生详情
  viewStudentDetail(e) {
    const student = e.currentTarget.dataset.student;
    if (student) {
      wx.showModal({
        title: `${student.name}的红花详情`,
        content: `总红花数：${student.totalFlowers}朵\n本周获得：${student.weekFlowers}朵\n本月获得：${student.monthFlowers}朵\n最近活动：${student.recentActivity}`,
        showCancel: false,
        confirmText: '知道了'
      });
    }
  },

  // 发放红花
  giveFlower(e) {
    const student = e.currentTarget.dataset.student;
    if (student) {
      wx.showModal({
        title: '发放红花',
        content: `给${student.name}发放红花奖励？`,
        confirmText: '发放',
        cancelText: '取消',
        success: (res) => {
          if (res.confirm) {
            wx.showToast({
              title: '红花发放成功',
              icon: 'success'
            });
            // 刷新数据
            this.loadStudentRanking();
          }
        }
      });
    }
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.loadStudentRanking().then(() => {
      wx.stopPullDownRefresh();
    }).catch(() => {
      wx.stopPullDownRefresh();
    });
  }
});




